home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
UTILITIE
/
CPU_MEMO
/
3468.ZIP
/
POPUP.ZIP
/
WINDOWS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-06-30
|
9KB
|
267 lines
/*****************************************************************************
* Module : WINDOWS.C *
*----------------------------------------------------------------------------*
* Program : SCREEN.LIB *
*----------------------------------------------------------------------------*
* Author : M.R.Watson. Copyright (c) M.R.Watson 1987 *
*----------------------------------------------------------------------------*
* Last Updated : 17/06/88 *
*----------------------------------------------------------------------------*
* Synopsis *
* ======== *
* A "mini-window" system designed specifically for use in programs which *
* must be as small as possible. The screen and window handling is very *
* basic at the moment, and thus takes up little room. It is envisioned that *
* popup programs would use this module. *
*----------------------------------------------------------------------------*
* Note : Before using any functions in this module, you must first call the *
* "monitor()" function, which sets up the screen RAM start etc etc. *
*****************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <dos.h>
#include <standard.h>
/* These next two are used in the SCREENIO.ASM module. */
WORD DisplaySegment, DisplayStatus ;
/*****************************************************************************
* BYTE *SaveBox( BYTE *ptr, int top, lft, bot, rgt ) *
*----------------------------------------------------------------------------*
* If "p" is not NULL, it is used instead of the malloced area mentioned next.*
* Mallocs an area of memory large enough to hold the area of screen given, *
* copies the area of screen into it, and returns a pointer to it. *
*----------------------------------------------------------------------------*
* Returns a pointer to the saved area of screen. *
* Returns NULL if bad arguments or out of memory. *
*****************************************************************************/
BYTE *
SaveBox( ptr, top, lft, bot, rgt )
BYTE *ptr ;
{
int len1, len2, i ;
BYTE *p, *q ;
if (DisplaySegment == 0)
{
printf("You have not called the monitor() function!\n");
exit(FAIL);
}
if ( bot < top || rgt < lft )
return(NULL);
len1 = rgt - lft + 1 ;
len2 = len1 * 2 ;
if (ptr)
p = q = ptr ;
else if ( ( q = p = malloc( len2 * ( top - bot + 1 ) ) ) == NULL )
return(NULL);
for ( i = top ; i <= bot ; i++, p += len2 )
ReadScreen( i, lft, p, len1 );
return(q);
}
/*****************************************************************************
* RestoreBox( BYTE *ptr, int top, lft, bot, rgt ) *
*----------------------------------------------------------------------------*
* Given a saved area of screen (saved by SaveBox), pointed to by "ptr", *
* restores the saved area to the given area bounded by top,lft,bot,rgt. *
*****************************************************************************/
RestoreBox( ptr, top, lft, bot, rgt )
BYTE *ptr ;
{
int len1, len2, i ;
if ( bot < top || rgt < lft || ptr == NULL )
return(ERROR);
len1 = rgt - lft + 1 ;
len2 = len1 * 2 ;
for ( i = top ; i <= bot ; i++, ptr += len2 )
WriteScreen( i, lft, ptr, len1 );
return(SUCCEED);
}
/*****************************************************************************
* ClearBox( int top, lft, bot, rgt, colour ) *
*----------------------------------------------------------------------------*
* Clears the given area to the given colour, using spaces. *
* (Thus to get a colour other that black, use a non-black background colour) *
*****************************************************************************/
ClearBox( top, lft, bot, rgt, colour )
{
WORD buf[80] ;
WORD x ;
register i, len ;
if ( bot < top || rgt < lft || colour < 0 )
return(ERROR);
len = min( rgt - lft + 1, 80 );
x = ( (BYTE)colour << 8 ) + (BYTE)' ' ;
for ( i = 0 ; i < len ; i++ )
buf[i] = x ;
for ( i = top ; i <= bot ; i++ )
WriteScreen( i, lft, buf, len );
}
/*****************************************************************************
* border( int top, lft, bot, rgt, char *title, *note, int colour ) *
*----------------------------------------------------------------------------*
* Draws a nice border around the given area of screen. *
* The border is drawn with spaces. The border and title are given the *
* attribute "colour". This should be something like black on <some colour>. *
*****************************************************************************/
border( top, lft, bot, rgt, title, note, colour )
char *title, *note ;
{
WORD buf[80] ;
register i, len ;
WORD x ;
if ( bot < top || rgt < lft || colour < 0 )
return(ERROR);
len = min( rgt - lft + 1, 80 );
x = ( (BYTE)colour << 8 ) + (BYTE)' ' ;
for ( i = 0 ; i < len ; i++ )
buf[i] = x ;
WriteScreen( top, lft, buf, len );
WriteScreen( bot, lft, buf, len );
for ( i = top ; i <= bot ; i++ )
{
WriteScreen( i, lft, buf, 1 );
WriteScreen( i, rgt, buf, 1 );
}
if (title)
PrintCentered( top, lft, rgt, title, colour );
if (note)
PrintCentered( bot, lft, rgt, note, colour );
return(SUCCEED);
}
/*****************************************************************************
* PrintCentered( int row, lft, rgt, char *text, int colour ) *
*----------------------------------------------------------------------------*
* Writes the given text in the given colour centrally on the given row. *
*****************************************************************************/
PrintCentered( row, lft, rgt, text, colour )
char *text ;
{
int len1, len2 ;
if ( rgt < lft || colour < 0 || text == NULL )
return(ERROR);
len1 = strlen(text);
len2 = rgt - lft + 1 ;
if ( len2 < len1 )
return(ERROR);
prn( row, lft + (len2-len1)/2, text, colour );
return(SUCCEED);
}
/*****************************************************************************
* prn( int row, int col, char *text, int colour ) *
*----------------------------------------------------------------------------*
* Prints the specified text at the given position in the required colour. *
*****************************************************************************/
prn( row, col, text, colour )
char *text ;
{
WORD buf[80] ;
WORD c ;
int n, i ;
register BYTE *p ;
register WORD *q ;
if ( text == NULL || colour < 0 )
return(ERROR);
c = (WORD)colour << 8 ;
n = min( strlen(text), 80 );
for ( p = text, q = buf, i = 0 ; i < n ; i++, p++, q++ )
*q = *p + c ;
WriteScreen( row, col, buf, n );
return(SUCCEED);
}
/*****************************************************************************
* monitor(void) : Get monitor type, set display segment & status port *
*----------------------------------------------------------------------------*
* Returns 0 for mono, 1 for CGA, 2 for EGA mono, 3 for EGA colour monitor. *
*****************************************************************************/
monitor()
{
union REGS regs ;
int86( 0x11, ®s, ®s );
switch( regs.x.ax & 0x30 )
{
case 48 :
DisplaySegment = 0xB000 ;
DisplayStatus = 0x03BA ;
return(0); /* mono monitor */
case 16 :
case 32 :
DisplaySegment = 0xB800 ;
DisplayStatus = 0x03DA ;
return(1); /* color monitor */
case 00 : /* ega card... */
if ( peekb( 0x40, 0x87 ) & 1 ) /* ...in mono mode */
{
DisplaySegment = 0xB000 ;
DisplayStatus = 0x03BA ;
return(2);
}
else /* ...in colour mode */
{
DisplaySegment = 0xB800 ;
DisplayStatus = 0x03DA ;
return(3);
}
}
}